package com.rock.code.common.pager;

import com.github.pagehelper.util.StringUtil;
import com.rock.code.base.domain.Constants;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * The type Page.
 *
 * @author kongqiang
 * @date 2017.04.12
 */
public class Page implements Serializable {

    /**
     * The constant serialVersionUID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * The Flag: flag is false to only use order
     */
    protected  boolean flag = true;

    /**
     * 当前页数
     */
    protected int page;
    /**
     * 每页显示行数
     */
    protected int rows;
    /**
     * 排序字段
     */
    protected String sidx;
    /**
     * 排序方式
     */
    protected String sord;

    /**
     * Instantiates a new Page.
     *
     * @param pageSize the page size
     * @param page     the page
     */
    public Page(int pageSize, int page) {
        this.rows = pageSize;
        this.page = page;
    }

    public Page(String sidx, String sord) {
        this.flag = false;
        this.sidx = sidx;
        this.sord = sord;
    }

    /**
     * Instantiates a new Page.
     *
     * @param pageSize the page size
     * @param page     the page
     * @param sidx     the sidx
     * @param sord     the sord
     */
    public Page(int pageSize, int page, String sidx, String sord) {
        this.rows = pageSize;
        this.page = page;
        this.sidx = sidx;
        this.sord = sord;
    }

    /**
     * Instantiates a new Page.
     *
     * @param request the request
     */
    public Page(HttpServletRequest request) {
        this.page = Integer.parseInt(StringUtil.isEmpty(request.getParameter(Constants.PAGE_KEY)) ?
                Constants.PAGE_DEFAULT_VALUE : request.getParameter(Constants.PAGE_KEY));
        this.rows = Integer.parseInt(StringUtil.isEmpty(request.getParameter(Constants.ROWS_KEY)) ?
                Constants.ROWS_DEFAULT_VALUE : request.getParameter(Constants.ROWS_KEY));
        this.sidx = StringUtil.isEmpty(Constants.SORT_KEY) ? "" : request.getParameter(Constants.SORT_KEY);
        this.sord = StringUtil.isEmpty(Constants.ORDER_KEY) ? "" : request.getParameter(Constants.ORDER_KEY);
    }

    /**
     * Is flag.
     *
     * @return the boolean
     * @author kongqiang
     * @date 2018.01.02
     */
    public boolean isFlag() {
        return flag;
    }

    /**
     * Sets flag.
     *
     * @param flag the flag
     * @author kongqiang
     * @date 2018.01.02
     */
    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    /**
     * Gets page.
     *
     * @return the page
     * @author kongqiang
     * @date 2018.01.02
     */
    public int getPage() {
        return page;
    }

    /**
     * Sets page.
     *
     * @param page the page
     * @author kongqiang
     * @date 2018.01.02
     */
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * Gets rows.
     *
     * @return the rows
     * @author kongqiang
     * @date 2018.01.02
     */
    public int getRows() {
        return rows;
    }

    /**
     * Sets rows.
     *
     * @param rows the rows
     * @author kongqiang
     * @date 2018.01.02
     */
    public void setRows(int rows) {
        this.rows = rows;
    }

    /**
     * Gets sidx.
     *
     * @return the sidx
     * @author kongqiang
     * @date 2018.01.02
     */
    public String getSidx() {
        return sidx;
    }

    /**
     * Sets sidx.
     *
     * @param sidx the sidx
     * @author kongqiang
     * @date 2018.01.02
     */
    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

    /**
     * Gets sord.
     *
     * @return the sord
     * @author kongqiang
     * @date 2018.01.02
     */
    public String getSord() {
        return sord;
    }

    /**
     * Sets sord.
     *
     * @param sord the sord
     * @author kongqiang
     * @date 2018.01.02
     */
    public void setSord(String sord) {
        this.sord = sord;
    }
}
